/**
 * 
 */
package com.ntv.service.content;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;

import com.ntv.domain.IContent;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.content.IContentDao;
import com.ntv.service.DomainException;

/**
 * @author Vedamoorthi Vairam
 *
 */
public class ContentAppService implements IContentAppService {

	private IContentDao contentDao;
	
	/* (non-Javadoc)
	 * @see com.ntv.service.content.IContentAppService#create(com.ntv.domain.IContent)
	 */
	public IContent create(IContent content) throws DomainException {
		try {
			return contentDao.create(content);
		}catch (PersistentException ex) {
			throw new DomainException(ex);
		}
	}

	/* (non-Javadoc)
	 * @see com.ntv.service.content.IContentAppService#findConent(java.lang.Long)
	 */
	public IContent findConent(Long contentId) throws DomainException {
		try {
			return contentDao.findConent(contentId);
		}catch (PersistentException ex) {
			throw new DomainException(ex);
		}
	}

	/**
	 * @param contentDao the contentDao to set
	 */
	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}
	
	@Transactional(readOnly=true)
	public void streamImage(final Long contentId, final OutputStream contentStream) throws DomainException {
		
		try {
			IContent content = contentDao.findConent(contentId);			
			FileCopyUtils.copy(content.getContent(), contentStream);
		}
		catch (PersistentException ex) {
			throw new DomainException(ex);
		} catch (IOException ex) {
			throw new DomainException(ex);
		}		
	}

	public void streamWishesImage(final Long id, OutputStream  outputStream) throws DomainException
	{
		try {
			InputStream is = contentDao.findWishesContent(id);
			FileCopyUtils.copy(is, outputStream);
		}
		catch (PersistentException ex) {
			throw new DomainException(ex);
		} catch (IOException ex) {
			throw new DomainException(ex);
		}	
	}

	public void streamObituaryImage(final Long id, OutputStream  outputStream) throws DomainException
	{
		try {
			InputStream is = contentDao.findObituaryContent(id);
			FileCopyUtils.copy(is, outputStream);
		}
		catch (PersistentException ex) {
			throw new DomainException(ex);
		} catch (IOException ex) {
			throw new DomainException(ex);
		}	
	}
	
	@Transactional
	public void storeImage(
	    final String name, final InputStream contentStream, final int contentLength, final String description)
	    throws DomainException {
		throw  new UnsupportedOperationException("yet to implement this method");
	}

	
}
